<?php 


include_once dirname(__FILE__)."/../util/MysqlUtil.php";
include_once dirname(__FILE__)."/../util/DakaqiUtil.php";

class MemberModel{


	// 通过手机号码、密码检查账号是否存在
	function checkExists($mobile,$password){

		$sql="select memberID FROM member WHERE mobile='$mobile' AND password='$password' order by id desc LIMIT 1";

		$mysql=new MysqlUtil();

		$data=$mysql->query($sql);

		if(empty($data)) return "";

		return $data[0]["memberID"];

	}


	// 绑定微信账号
	public function bandByMid($mid,$openid){

		$sql="update member set weixin_openid='$openid' where memberID='$mid'";

		$mysql=new MysqlUtil();

		$mysql->update($sql);
	}


	// 会员注册
	public function regist($memberInfo){

		return DakaqiUtil::registMember($memberInfo);
	}


	// 通过手机号码获取mid
	public function getMidByMobile($mobile){

		$sql="select memberID FROM member WHERE mobile='$mobile' order by id desc LIMIT 1";

		$mysql=new MysqlUtil();

		$data=$mysql->query($sql);

		if(empty($data)) return "";

		return $data[0]["memberID"];
	}


	// 获取会员报名参加的3个活动，按报名时间倒叙排列,过滤掉已经结束的活动
	public function joinActivitys($memberid){

		$sql="select a.id,a.`name`,a.demo 
			from activity_apply as s ,activity as a
			where a.id=s.activity
			and s.member=$memberid
			and s.delStatus=0
			AND s.status=2
			and a.delStatus=0
			and a.endDay >= curdate()
			order by s.createTime desc LIMIT 3
			";

		$mysql=new MysqlUtil();

		$data=$mysql->query($sql);

		return $data;

	}

	// 获取会员信息
	public function info($id){

		$sql="select id,nickName,realName,mobile,headUrl,headUrlm,headUrls,userQR,sex,province,city,district,volunteerTime,allTimes
		from member where id=$id and delStatus=0";

		$mysql=new MysqlUtil();

		$data=$mysql->query($sql);

		if(empty($data)) return "";

		return $data[0];

	}


	// 获取会员二维码图片url
	public function getPicCodeUrl($openId){

		$sql="select userQR from member where weixin_openid='$openId' order by id desc LIMIT 1";

		$mysql=new MysqlUtil();

		$data=$mysql->query($sql);

		if(empty($data)) return "";

		return $data[0]['userQR'];

	}

	// 使用openid 获取 member 表中的id
	public function getMemberIdByOpenId($openid){

		$sql="select id from member where weixin_openid='$openid' and delStatus=0 order by id desc LIMIT 1";

		$mysql=new MysqlUtil();

		$data=$mysql->query($sql);

		if(empty($data)) return "";

		return $data[0]['id'];
	}



	// 使用 openid 获取 member表中的 memberId
	public function getMidByOpenId($openid){

		$sql="select memberID from member where weixin_openid='$openid' and delStatus=0 order by id desc LIMIT 1";

		$mysql=new MysqlUtil();

		$data=$mysql->query($sql);

		if(empty($data)) return "";

		return $data[0]['memberID'];
	}



	// 检查会员是否已绑定
	public function checkBand($openid){

		$sql="select id from member where weixin_openid='$openid' and delStatus=0";

		$mysql=new MysqlUtil();

		$data=$mysql->query($sql);

		return !empty($data);
	}

	// 绑定会员 openid
	// public function band($mobile,$password,$openid){

	// 	$sql="update member set weixin_openid='$openid' where mobile='$mobile' and password='$password'";

	// 	$mysql=new MysqlUtil();

	// 	$mysql->update($sql);


	// }


    public function all(){


        return null;

    }





}

